package com.steven.ourcontest.team.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.steven.ourcontest.R
import com.steven.ourcontest.me.widget.GlideRoundTransform
import com.steven.ourcontest.net.NetConstants
import com.steven.ourcontest.team.bean.TeamApply

class ApplicationAdapter(
    private val context: Context,
    private val applyClickListener: ((applyId: Long, pass: Boolean) -> Unit)
) :
    RecyclerView.Adapter<ApplicationViewHolder>() {
    val listData by lazy {
        ArrayList<TeamApply>()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ApplicationViewHolder {
        val inflater =
            LayoutInflater.from(context).inflate(R.layout.item_team_application, parent, false)
        return ApplicationViewHolder(inflater)
    }

    override fun getItemCount(): Int {
        return listData.size
    }

    override fun onBindViewHolder(holder: ApplicationViewHolder, position: Int) {
        val itemData = listData[position]
        holder.apply {
            tv_name?.text = itemData.uname
            Glide.with(context)
                .load(NetConstants.ICON_BASE_URL + itemData.iconUrl)
                .error(R.drawable.ic_avatar)
                .apply(RequestOptions.bitmapTransform(GlideRoundTransform(context, 10))).into(iv_icon)
            tv_team_name?.text = itemData.teamName
            tv_agree?.setOnClickListener {
                applyClickListener.invoke(itemData.id, true)
            }
            tv_refuse?.setOnClickListener {
                applyClickListener.invoke(itemData.id, true)
            }
        }
    }
}

class ApplicationViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    val iv_icon: ImageView = itemView.findViewById(R.id.iv_icon)
    val tv_name: TextView? = itemView.findViewById(R.id.tv_name)
    val tv_time: TextView? = itemView.findViewById(R.id.tv_time)
    val tv_team_name: TextView? = itemView.findViewById(R.id.tv_team_name)
    val tv_agree: TextView? = itemView.findViewById(R.id.tv_agree)
    val tv_refuse: TextView? = itemView.findViewById(R.id.tv_refuse)
}
